<?php
include_once '../Login/databaseconnection.php';
$SchName=$_POST['SchName'];
$name=explode(",",$SchName);
$nam=$name[0];
$tn=count($name);
for($i=1;$i<$tn;$i++)
{
if(empty($adr))
{
$adr=$name[$i];
}
else
{
$adr=$adr.",".$name[$i];
}
}
$results= mysql_query("SELECT * FROM school Where Name='$nam' AND Address='$adr'");
if($row = mysql_fetch_array($results))
{
 $Census_Number=$row['Census_Number'];
 $School_Id=$row['School_Id'];
}
  
if($Census_Number!="")
	   {
   if($_SESSION['Access_Type']=="Administrator")
   {
    
	$result=mysql_query("SELECT * FROM school WHERE Census_Number='$Census_Number'");
	$resultexa=mysql_query("SELECT * FROM exam WHERE School_Id='$Own_School_Id'");
	$resultpro=mysql_query("SELECT * FROM programmes WHERE School_Id='$Own_School_Id'");
	$resultcom=mysql_query("SELECT * FROM school competition School_Id='$Own_School_Id'");
	if($result)
  {
	if($row=mysql_fetch_array($result))
		 {
		 $Principal_Name=$row['Principal_Name'];
		 $School_Id=$row['School_Id'];
		 $Account_Number=$row['Account_Number'];
		 $Web_Address=$row['Web_Address'];
		 $Name=$row['Name'];
		 $Tp_Number=$row['Tp_Number'];
		 $Medium=$row['Medium'];
		 $E_Mail=$row['E_Mail'];
		 $Fax_Number=$row['Fax_Number'];
		 $Address=$row['Address'];
		 $Grade_Range=$row['Grade_Range'];
		 $Type=$row['Type'];
		 $District=$row['District'];
		 $Status=$row['Status'];
		 $Province=$row['Province'];
		 }
if(mysql_num_rows($result)==0)
{
 $_SESSION['errornorow']="Very sorry there is no result,Please check your input and try again!";
   }
else
   {
   $Accessed_By=$_SESSION['NIC_Number'];
   date_default_timezone_set("Europe/Paris");
    $t_d=date("Y-m-d");
   if($row=mysql_fetch_array($result))
   {
$Principal_Name=$row['Principal_Name'];
$Zone=$row['Zone'];
$Census_Number=$row['Census_Number'];
$Medium=$row['Medium'];
$Female_Students=$row['Female_Students'];
$Male_Students=$row['Male_Students'];
$S_Pro=$row['Special_Projects'];
$Medium=$row['Medium'];
$Account_Number=$row['Account_Number'];
$School_Id=$row['School_Id'];
$Name=$row['Name'];
$Address=$row['Address'];
$Division=$row['Division'];
$District=$row['District'];
$Province=$row['Province'];
$Status=$row['Status'];
$Type=$row['Type'];
$Grade_Range=$row['Grade_Range'];
$Tp_Number=$row['Tp_Number'];
$Fax_Number=$row['Fax_Number'];
$E_Mail=$row['E_Mail'];
$Web_Address=$row['Web_Address'];
$Congeniality=$row['Congeniality'];
$Function=$row['Function'];
$Ethinicity=$row['Ethinicity'];
$Last_Updated_Date=$row['Last_Updated_Date'];  
   }
   if($resultcom)
   {
  while($rowcom=mysql_fetch_array($resultcom))
   {
   if(empty($Competition_Id))
   {
   $Competition_Id=$rowcom['Competition_Id'];
   }
   else
   {
   $Competition_Id=$Competition_Id.",".$rowcom['Competition_Id'];
   }
   if(empty($No_Of_Students))
   {
   $No_Of_Students=$rowcom['No_Of_Students'];
   }
   else
   {
   $No_Of_Students=$No_Of_Students.",".$rowcom['No_Of_Students'];
   }
   if(empty($No_Of_Teachers))
   {
   $No_Of_Teachers=$rowcom['No_Of_Teachers'];
   }
   else
   {
   $No_Of_Teachers=$No_Of_Teachers.",".$rowcom['No_Of_Teachers'];
   }
   if(empty($Result))
   {
   $Result=$rowcom['Result'];
   }
   else
   {
   $Result=$Result.",".$rowcom['Result'];
   }
   if(empty($Date))
   {
   $Date=$rowcom['Date'];
   }
   else
   {
   $Date=$Date.",".$rowcom['Date'];
   }
   if(empty($Scope))
   {
   $Scope=$rowcom['Scope'];
   }
   else
   {
   $Scope=$Scope.",".$rowcom['Scope'];
   }
   if(empty($Winner))
   {
   $Winner=$rowcom['Winner'];
   }
   else
   {
   $Winner=$Winner.",".$rowcom['Winner'];
   } 
}
}
if($resultpro)
   {
 while($rowpro=mysql_fetch_array($resultpro))
   {
   if(empty($Required_Teachers))
   {
   $Required_Teachers=$rowpro['Required_Teachers'];
   }
   else
   {
   $Required_Teachers=$Required_Teachers.",".$rowpro['Required_Teachers'];
   }
     if(empty($Grade))
   {
  $Grade=$rowpro['Grade'];
   }
   else
   {
   $Grade=$Grade.",".$rowpro['Grade'];
   }
   if(empty($Pro_Id))
   {
  $Pro_Id=$rowpro['Programme_Id'];
   }
   else
   {
   $Pro_Id=$Pro_Id.",".$rowpro['Programme_Id'];
   }
   if(empty($Year))
   {
   $Year=$rowpro['Year'];
   }
   else
   {
   $Year=$Year.",".$rowpro['Year'];
   }
   if(empty($No_F_Stu))
   {
   $No_F_Stu=$rowpro['No_Of_Female_Students'];
   }
   else
   {
   $No_F_Stu=$No_F_Stu.",".$rowpro['No_Of_Female_Students'];
   }
    if(empty($No_M_Stu))
   {
   $No_M_Stu=$rowpro['No_Of_Male_Students'];
   }
   else
   {
   $No_M_Stu=$No_M_Stu.",".$rowpro['No_Of_Male_Students'];
   }
   if(empty($No_Tra_tea))
   {
   $No_Tra_tea=$rowpro['Teachers_In_Training'];
   }
   else
   {
   $No_Tra_tea=$No_Tra_tea.",".$rowpro['Teachers_In_Training'];
   }
   if(empty($Tra_tea))
   {
   $Tra_tea=$Tra_tea.",".$rowpro['Trained_Teachers'];
   }
   else
   {
   $Tra_tea=$Tra_tea.",".$rowpro['Trained_Teachers'];
   }
   if(empty($Avai_Book))
   {
   $Avai_Book=$rowpro['Availability_Books'];
   }
   else
   {
   $Avai_Book=$Avai_Book.",".$rowpro['Availability_Books'];
   }
   if(empty($Avai_Tea_Gui))
   {
   $Avai_Tea_Gui=$rowpro['Availability_Tguides'];
   }
   else
   {
   $Avai_Tea_Gui=$Avai_Tea_Gui.",".$rowpro['Availability_Tguides'];
   }
}
}
if($resultexa)
   {
while($rowexa=mysql_fetch_array($resultexa))
   {
   if(empty($Exam_Name))
   {
   $Exam_Name=$rowexa['Exam_Name'];
   }
   else
   {
   $Exam_Name=$Exam_Name.",".$rowexa['Exam_Name'];
   }
   if(empty($Exa_Year))
   {
   $Exa_Year=$rowexa['Year'];
   }
   else
   {
   $Exa_Year=$Exa_Year.",".$rowexa['Year'];
   }
   if(empty($No_Of_Sat_Students))
   {
   $No_Of_Sat_Students=$rowexa['No_Of_Sat_Students'];
   }
   else
   {
   $No_Of_Sat_Students=$No_Of_Sat_Students.",".$rowexa['No_Of_Sat_Students'];
   }
   if(empty($No_Of_Passed_Students))
   {
   $No_Of_Passed_Students=$rowexa['No_Of_Passed_Students'];
   }
   else
   {
   $No_Of_Passed_Students=$No_Of_Passed_Students.",".$rowexa['No_Of_Passed_Students'];
   }

	}
}
$sql=mysql_query("INSERT INTO deleted_school(Competition_Id,No_Of_Students,No_Of_Teachers,Result,Date,Scope,Winner,Required_Teachers,Grade,Programme_Id,Year,No_Of_Students,Teachers_In_Training,Trained_Teachers,Availability_Books,Availability_Tguides,Congeniality,Function,Ethinicity,School_Id,Zone,Division,Province,District,Census_Number,Name,Principal_Name,Status,Type,Grade_Range,Medium,Female_Students,Male_Students,Tp_Number,Fax_Number,E_Mail,Web_Address,Special_Projects,Address,Account_Number,Deleted_date,Accessed_By,Last_Updated_Date,Exam_Name,Exa_Year,No_Of_Sat_Students,No_Of_Passed_Students) VALUES('$Competition_Id','$No_Of_Students','$No_Of_Teachers','$Result','$Date','$Scope','$Winner','$Required_Teachers','$Grade','$Pro_Id','$Year','$Sch_Id','$No_Stu','$No_Tra_tea','$Tra_tea','$Avai_Book','$Avai_Tea_Gui','$Congeniality','$Function','$Ethinicity','$School_Id','$Zone','$Division','$Province','$District','$Census_Number','$Name','$Principal_Name','$Status','$Type','$Grade_Range','$Medium','$Female_Students','$Male_Students','$Tp_Number','$Fax_Number','$E_Mail','$Web_Address','$S_Pro','$Address','$Account_Number','$t_d','$Accessed_By','$Last_Updated_Date','$Exam_Name','$Exa_Year','$No_Of_Sat_Students','$No_Of_Passed_Students')");
if($sql){
   $del=mysql_query("DELETE FROM school WHERE Census_Number='$Census_Number'");
	if($del)
  {
$_SESSION['error']="This record is successfully deleted!";
if($resultexa)
   {
		  while($rowtra=mysql_fetch_array($resultexa))
		 {
		  $delexa=mysql_query("DELETE FROM exam WHERE School_Id='$School_Id'");
		 }
		 }
		 	 if($resultpro)
   {
		 while($rowres=mysql_fetch_array($resultpro))
		 {
	  	$delpro=mysql_query("DELETE FROM programmes WHERE School_Id='$School_Id'");
		 }
		 }
		  	 if($resultcom)
   {
		 while($rowc=mysql_fetch_array($resultcom))
		 {
	  $delcom=mysql_query("DELETE FROM competition WHERE School_Id='$School_Id'");
		 }
		 }		 
  if(mysql_num_rows(mysql_query("SELECT * FROM school competition School_Id='$School_Id'"))!=0)
	  {
$_SESSION['error']=$_SESSION['error']." ,unable to delete this record from competition!";
  }  
  if(mysql_num_rows(mysql_query("SELECT * FROM exam WHERE School_Id='$School_Id'"))!=0)
	  {
 $_SESSION['error']=$_SESSION['error']." ,unable to delete this record from exam!";
   }
 if(mysql_num_rows(mysql_query("SELECT * FROM programmes WHERE School_Id='$School_Id'"))!=0)
	  {
 $_SESSION['error']=$_SESSION['error']." ,unable to delete this record from programmes!";
   }
  }

  if(!$del)
  {
  $School_Id='';
		 $Account_Number='';
		 $Web_Address='';
		 $Name='';
		 $Tp_Number='';
		 $Medium='';
		 $E_Mail='';
		 $Fax_Number='';
		 $Address='';
		 $Grade_Range='';
		 $Type='';
		 $District='';
		 $Status='';
		 $Province='';
		 $Principal_Name='';
 $_SESSION['error']="Sorry unable to delete this record!";
 mysql_query("DELETE FROM deleted_school WHERE Census_Number='$Census_Number' AND Deleted_Date='$t_d'");

  }
  }
     if(!$sql)
{
$School_Id='';
		 $Account_Number='';
		 $Web_Address='';
		 $Name='';
		 $Tp_Number='';
		 $Medium='';
		 $E_Mail='';
		 $Fax_Number='';
		 $Address='';
		 $Grade_Range='';
		 $Type='';
		 $District='';
		 $Status='';
		 $Province='';
		 $Principal_Name='';
 $_SESSION['error']="Sorry unable to delete this record!";
 }
  }
  }
else
  {
 $_SESSION['errornot']="Sorry there is no result!";
  }
  
}
}
if($Census_Number=="")
	   {
		   $_SESSION['errorin']="Please select the School's name!";
    
	   }
?>

